/**
 * Author: feixy
 * Date: 2019-08-16
 * Time: 17:56
 */

function update(arr = [], valueObj, predicate = () => true) {
  const newArr = arr.map((item, index) => {
    if (predicate(item, index)) {
      return { ...item, ...valueObj };
    }
    return item;
  });
  return newArr;
}

function toObject(arr = [], keyExtractor, valueExtractor = item => item) {
  const obj = {};
  arr.forEach(item => {
    obj[keyExtractor(item)] = valueExtractor(item);
  });
  return obj;
}

export default { update, toObject };
